package com.dolawing.phonecallapp.keepAliveUi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.dolawing.phonecallapp.R;

/**
 * Created by Amn on 2019/4/20.
 */

public class KeepActivity extends Activity {
    private ImageView logoutImg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //唤醒屏幕
        showOnLockScreen();
        WindowManager windowManager = (WindowManager) getApplicationContext()
                            .getSystemService(Context.WINDOW_SERVICE);
        int width = windowManager.getDefaultDisplay().getWidth();
        int height = windowManager.getDefaultDisplay().getHeight();
        Window window= getWindow();
        //设置activity 左上角
        // window.setGravity(Gravity.START | Gravity.TOP);
        window.setGravity(Gravity.TOP);
        // 创建宽高都 是 1 像素
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        attributes.width=width;
        attributes.height=WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.x=0;
        attributes.y=0;
        window.setAttributes(attributes);

        //显示 自定义界面
        setContentView(R.layout.view_onepoint_view);

        //设置弱引用
        KeepManager.getInstance().setKeep(this);
        //初台化View
        initView();
    }

    private void initView(){
        logoutImg = findViewById(R.id.dialog_logout_img);
        logoutImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeepManager.getInstance().finishKeep();
            }
        });
    }

    /**
     * 手动按了锁屏键，当前面的广播接收器再次去启动它的时候，屏幕并不会被唤起，
     * 所以我们需要在activity当中添加唤醒屏幕的代码，这里用的是电源锁
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            @SuppressLint("InvalidWakeLockTag")
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            wl.acquire();
            wl.release();
        }
    }

    /**
     * 唤醒屏幕:官方推荐
     */
    public void showOnLockScreen() {
        Log.i("唤醒屏幕","执行");
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION //hide navigationBar
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        this.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | //保持屏幕不息屏
                        WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|//点亮屏幕
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        //锁屏显示
        if (Build.VERSION.SDK_INT > 27){
            setShowWhenLocked(true);
        }else{
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        }
    }
}
